﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using BUS;
using DTO;
namespace GUI
{
    public partial class FormCapNhatBanAn : Form
    {
        private int id_ban_an;
        public int Id_Ban_An
        {
            get { return id_ban_an; }
            set { id_ban_an = value; }
        }
        public FormCapNhatBanAn()
        {
            InitializeComponent();
            txttenban.Text = FormDanhSachBanAn.Ten_Ban_An;
        }

        private void btthoat_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btdanhsach_Click(object sender, EventArgs e)
        {
            FormDanhSachBanAn frm = new FormDanhSachBanAn();
            this.Hide();
            frm.ShowDialog();
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {

        }

        private void FormCapNhatBanAn_Load(object sender, EventArgs e)
        {
            BanAnBUS bananbus = new BanAnBUS();
            BanAnDTO banan = bananbus.LayBanAn(FormDanhSachBanAn.ID);
            txttenban.Text = banan.Ten_Ban_An;
            //
            KhuVucBUS kvbus=new KhuVucBUS();
            List<KhuVucDTO> kv = kvbus.LayDanhSachKhuVuc();
            KhuVucDTO tem=new KhuVucDTO();
            for (int i = 0; i < kv.Count; i++)
            {
                if (kv[i].Id_Khu_Vuc == banan.Id_Khu_Vuc)
                {
                    tem.Id_Khu_Vuc = kv[i].Id_Khu_Vuc;
                    tem.Ten_Khu_Vuc = kv[i].Ten_Khu_Vuc;
                    kv.RemoveAt(i);
                    kv.Insert(0, tem);
                    break;
                }
            }
            cbkhuvuc.DataSource = kv;
            cbkhuvuc.DisplayMember = "Ten_Khu_Vuc";
            cbkhuvuc.ValueMember = "Id_Khu_Vuc";
        }

        private void btcapnhat_Click(object sender, EventArgs e)
        {
            if (txttenban.Text == null || txttenban.Text.Length == 0)
            {
                MessageBox.Show("Tên không được để trống");
                return;
            }
            BanAnBUS banan = new BanAnBUS();
            bool kt = banan.CapNhatBanAn(id_ban_an, txttenban.Text, int.Parse(cbkhuvuc.SelectedValue.ToString()));
            if (kt == true)
                MessageBox.Show("Cập nhật thành công");
            else
                MessageBox.Show("Cập nhật thất bại");
        }
    }
}
